package example.com.zcq.mymj.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import example.com.zcq.mymj.Adapter.LiShiAdapter;
import example.com.zcq.mymj.R;
import example.com.zcq.mymj.activity.ChengYuActivity;
import example.com.zcq.mymj.activity.XinHuaZiDianActivity;
import example.com.zcq.mymj.bean.LishiJinRi;
import example.com.zcq.mymj.net.HttpInterface;
import example.com.zcq.mymj.net.RetrofitHttp;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author zcq
 * @package example.com.zcq.mymj.fragment
 * @fileName MyMj
 * @description: 这个类的描述
 * @org 融信通达科技有限公司
 * @email @163.com
 * @date 2018/11/22  19:59
 */
public class Fragment1 extends Fragment implements View.OnClickListener {
    private RollPagerView mRollViewPager;
    private View view;
    /**
     * 金榜资料
     */
    private TextView mTxtTopbar;
    private RelativeLayout mLyTopBar;
    private RollPagerView mRollViewPagerLunbo;
    /**
     * You have to do the best what God gave you.
     */
    private TextView mTvEnglish;
    /**
     * 你必须尽力发挥上帝所赐予你的。
     */
    private TextView mTvChinese;
    private ImageView mIvHanzi;
    private ImageView mIvChengyu;
    private RecyclerView mRc1;
    private List<LishiJinRi.ResultBean> mResultBeanList = new ArrayList<>();
    private LiShiAdapter liShiAdapter;
    private ScrollView mScv;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fg1, container, false);
        mRollViewPager = (RollPagerView) view.findViewById(R.id.roll_view_pager_lunbo);
        //设置播放时间间隔
        mRollViewPager.setPlayDelay(5000);
        //设置透明度
        mRollViewPager.setAnimationDurtion(2000);
        //设置适配器
        mRollViewPager.setAdapter(new TestNormalAdapter());

        //设置指示器（顺序依次）
        //自定义指示器图片
        //设置圆点指示器颜色
        //设置文字指示器
        //隐藏指示器
        //mRollViewPager.setHintView(new IconHintView(this, R.drawable.point_focus, R.drawable.point_normal));
        mRollViewPager.setHintView(new ColorPointHintView(getActivity(), Color.BLUE, Color.WHITE));
        //mRollViewPager.setHintView(new TextHintView(this));
        //mRollViewPager.setHintView(null);
        initView(view);
        initData();
        return view;
    }

    private void initData() {
        Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
        t.setToNow(); // 取得系统时间。
        int year = t.year;
        int month = t.month+1;
        int date = t.monthDay;
        int hour = t.hour;    // 0-23
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("key", "93cf2d0a4c635257564096c163851601");
        hashMap.put("v", "1.0");
        hashMap.put("day", date + "");
        hashMap.put("month", month + "");
        HttpInterface h = RetrofitHttp.getSimpleRetrofit();
        h.lishijinri(hashMap).enqueue(new Callback<LishiJinRi>() {
            @Override
            public void onResponse(Call<LishiJinRi> call, Response<LishiJinRi> response) {
                LishiJinRi b = response.body();
                List<LishiJinRi.ResultBean> list = b.getResult();
                if (list != null) {
                    mResultBeanList.clear();
                    mResultBeanList.addAll(list);
                    liShiAdapter.notifyDataSetChanged();
                }

            }

            @Override
            public void onFailure(Call<LishiJinRi> call, Throwable t) {

            }
        });
    }

    private void initView(View view) {
        mTxtTopbar = (TextView) view.findViewById(R.id.txt_topbar);
        mLyTopBar = (RelativeLayout) view.findViewById(R.id.ly_top_bar);
        mRollViewPagerLunbo = (RollPagerView) view.findViewById(R.id.roll_view_pager_lunbo);
        mTvEnglish = (TextView) view.findViewById(R.id.tv_english);
        mTvChinese = (TextView) view.findViewById(R.id.tv_chinese);
        mIvHanzi = (ImageView) view.findViewById(R.id.iv_hanzi);
        mIvHanzi.setOnClickListener(this);
        mIvChengyu = (ImageView) view.findViewById(R.id.iv_chengyu);
        mIvChengyu.setOnClickListener(this);
        mRc1 = (RecyclerView) view.findViewById(R.id.rc_1);
        mScv = (ScrollView) view.findViewById(R.id.scv);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRc1.setLayoutManager(layoutManager);
        liShiAdapter = new LiShiAdapter(getActivity(), mResultBeanList);
        mRc1.setAdapter(liShiAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_hanzi:
                startActivity(new Intent(getActivity(), XinHuaZiDianActivity.class));
                break;
            case R.id.iv_chengyu:
                startActivity(new Intent(getActivity(), ChengYuActivity.class));
                break;
        }
    }


    private class TestNormalAdapter extends StaticPagerAdapter {

        private int[] imgs = {
                R.drawable.lunbo1,
                R.drawable.lunbo2,
                //                R.drawable.ccc,
        };


        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            view.setImageResource(imgs[position]);
            //            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            return view;
        }


        @Override
        public int getCount() {
            return imgs.length;
        }
    }



    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        //直接置顶，瞬间回到顶部，没有滚动过程，其中Y值可以设置为大于0的值，使Scrollview停在指定位置;
        mScv.scrollTo(0,0);
    }
}
